package com.foreveross.mofang.serviceconsole.application.dto;

import java.util.Date;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.openkoala.koala.springmvc.JsonTimestampSerializer;
import org.springframework.format.annotation.DateTimeFormat;

import com.foreveross.mofang.serviceconsole.core.Channel;
import com.foreveross.mofang.serviceconsole.core.ChannelGroup;
import com.foreveross.mofang.serviceconsole.core.Group;

public class ChannelDTO extends BaseDTO<Channel> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Size(max = 36, message = "编号不能超过36个字符")
	private String sn;

	@NotNull(message = "渠道名称不能为空!")
	@Size(max = 30, message = "名称不能超过30个字符")
	private String name;

	@NotNull(message = "渠道全称不能为空!")
	@Size(max = 150, message = "全名称不能超过150个字符")
	private String fullName;

	@NotNull(message = "数据库类型不能为空")
	@Size(max = 150, message = "数据库类型不能超过150个字符")
	private String dbTable;

	@NotNull(message = "入口url不能为空!")
	@Size(max = 150, message = "入口地址长度不能超过250个字符")
	@Pattern(regexp = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]", message = "url格式错误")
	private String entryUrl;

	@Size(max = 150, message = "渠道备注不能超过150字符!")
	private String remarks;

	@NotNull(message = "数据库表不能为空")
	@Size(max = 150, message = "数据库表不能超过150个字符")
	private String dbType;
	
	private String dbTypeName;

	@JsonSerialize(using=JsonTimestampSerializer.class)
	private Date createDate;
	
	// 是否使用代理IP
	private Boolean isUseProxy;

	// 创建人
	private String createUserName;

	private String createUserAccount;

	// 最后更新人
	private String lastUpdateUserName;

	private String lastUpdateUserAccount;
	
	@JsonSerialize(using=JsonTimestampSerializer.class)
	private Date lastUpdate;
	
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date startDate;
	
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date endDate;
	
	private Long group;
	
	private String groupName;
	

	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public String getDbTable() {
		return dbTable;
	}

	public void setDbTable(String dbTable) {
		this.dbTable = dbTable;
	}

	public String getEntryUrl() {
		return entryUrl;
	}

	public void setEntryUrl(String entryUrl) {
		this.entryUrl = entryUrl;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getDbType() {
		return dbType;
	}

	public String getDbTypeName() {
		return dbTypeName;
	}

	public void setDbTypeName(String dbTypeName) {
		this.dbTypeName = dbTypeName;
	}

	public void setDbType(String dbType) {
		this.dbType = dbType;
	}
	
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Boolean getIsUseProxy() {
		return isUseProxy;
	}

	public void setIsUseProxy(Boolean isUseProxy) {
		this.isUseProxy = isUseProxy;
	}

	public String getCreateUserName() {
		return createUserName;
	}

	public void setCreateUserName(String createUserName) {
		this.createUserName = createUserName;
	}

	public String getCreateUserAccount() {
		return createUserAccount;
	}

	public void setCreateUserAccount(String createUserAccount) {
		this.createUserAccount = createUserAccount;
	}

	public String getLastUpdateUserName() {
		return lastUpdateUserName;
	}

	public void setLastUpdateUserName(String lastUpdateUserName) {
		this.lastUpdateUserName = lastUpdateUserName;
	}

	public String getLastUpdateUserAccount() {
		return lastUpdateUserAccount;
	}

	public void setLastUpdateUserAccount(String lastUpdateUserAccount) {
		this.lastUpdateUserAccount = lastUpdateUserAccount;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Long getGroup() {
		return group;
	}

	public void setGroup(Long group) {
		this.group = group;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	@Override
	public void domain2Dto(Channel channel) {
		super.setId(channel.getId());
		this.setGroup(channel.getGroup().getId());
		this.setGroupName(channel.getGroup().getName());
		this.setSn(channel.getSn());
		this.setName(channel.getName());
		this.setFullName(channel.getFullName());
		this.setDbTable(channel.getDbTable());
		this.setDbType(channel.getDbType().name());
		this.setDbTypeName(channel.getDbType().getLable());
		this.setRemarks(channel.getRemarks());
		this.setEntryUrl(channel.getEntryUrl());
		this.setCreateDate(channel.getCreateDate());
		this.setCreateUserAccount(channel.getUser().getUserAccount());
		this.setCreateUserName(channel.getUser().getName());
		this.setLastUpdateUserAccount(channel.getLastUpdateUser().getUserAccount());
		this.setLastUpdateUserName(channel.getLastUpdateUser().getName());
		this.setLastUpdate(channel.getLastUpdate());
		this.setIsUseProxy(channel.getIsUseProxy());

	}

	@Override
	public void dto2Domain(Channel channel) {
		if (StringUtils.isNotBlank(this.sn)) {
			channel.setSn(this.sn);
		}
		
		if (this.group != null) {
			channel.setGroup(Group.get(ChannelGroup.class, group));
		}
		
		channel.setName(this.name);
		channel.setFullName(this.fullName);
		channel.setDbTable(this.dbTable);
		channel.setDbType(Channel.DBType.valueOf(this.dbType));
		channel.setEntryUrl(this.entryUrl);
		channel.setRemarks(this.remarks);
		channel.setIsUseProxy(this.isUseProxy);
	}

}